package io.github.eric.urpc.client;

import io.github.eric.urpc.core.config.AbstractConfig;
import io.github.eric.urpc.core.config.AbstractContext;
import io.github.eric.urpc.core.thread.NamePrefixThreadFactory;

import java.util.Collections;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ClientContext extends AbstractContext implements ClientConfig {
    private String serverHost;
    private int serverPort;
    private int heartBeatInterval;
    private int socketConnectTimeout;
    // 等待客户端连接初始化(注册应用信息给服务端)完成的超时时间
    private int connectionInitTimeout;
    private int connectionPoolMaxTotal;
    private int connectionPoolMaxIdle;
    private int connectionPoolMinIdle;
    private int connectionPoolTimeBetweenEvictionRuns;
    private int connectionPoolMaxWait;
    private final ExecutorService heartBeatExecutor;

    ClientContext(ExecutorService rpcExecutor) {
        super(AbstractConfig.CLIENT, rpcExecutor);
        addRpcControllers(Collections.singletonList(new ClientCoreRpcController(this)));
        heartBeatExecutor = Executors.newSingleThreadExecutor(new NamePrefixThreadFactory("urpc-client-heartbeat"));
    }

    @Override
    public String getServerHost() {
        return serverHost;
    }

    @Override
    public int getServerPort() {
        return serverPort;
    }

    public void setServerHost(String serverHost) {
        this.serverHost = serverHost;
    }

    public void setServerPort(int serverPort) {
        this.serverPort = serverPort;
    }

    @Override
    public int getHeartBeatInterval() {
        return heartBeatInterval;
    }

    public void setHeartBeatInterval(int heartBeatInterval) {
        this.heartBeatInterval = heartBeatInterval;
    }

    @Override
    public int getSocketConnectTimeout() {
        return socketConnectTimeout;
    }

    public void setSocketConnectTimeout(int connectTimeout) {
        this.socketConnectTimeout = connectTimeout;
    }

    @Override
    public int getConnectionInitTimeout() {
        return connectionInitTimeout;
    }

    public void setConnectionInitTimeout(int connectionInitTimeout) {
        this.connectionInitTimeout = connectionInitTimeout;
    }

    @Override
    public int getConnectionPoolMaxTotal() {
        return connectionPoolMaxTotal;
    }

    public void setConnectionPoolMaxTotal(int connectionPoolMaxTotal) {
        this.connectionPoolMaxTotal = connectionPoolMaxTotal;
    }

    @Override
    public int getConnectionPoolMaxIdle() {
        return connectionPoolMaxIdle;
    }

    public void setConnectionPoolMaxIdle(int connectionPoolMaxIdle) {
        this.connectionPoolMaxIdle = connectionPoolMaxIdle;
    }

    @Override
    public int getConnectionPoolMinIdle() {
        return connectionPoolMinIdle;
    }

    public void setConnectionPoolMinIdle(int connectionPoolMinIdle) {
        this.connectionPoolMinIdle = connectionPoolMinIdle;
    }

    @Override
    public int getConnectionPoolTimeBetweenEvictionRuns() {
        return connectionPoolTimeBetweenEvictionRuns;
    }

    public void setConnectionPoolTimeBetweenEvictionRuns(int connectionPoolTimeBetweenEvictionRuns) {
        this.connectionPoolTimeBetweenEvictionRuns = connectionPoolTimeBetweenEvictionRuns;
    }

    @Override
    public int getConnectionPoolMaxWait() {
        return connectionPoolMaxWait;
    }

    public void setConnectionPoolMaxWait(int connectionPoolMaxWait) {
        this.connectionPoolMaxWait = connectionPoolMaxWait;
    }

    @Override
    public ClientConnectionManager getConnectionManager() {
        return (ClientConnectionManager) super.getConnectionManager();
    }

    public ExecutorService getHeartBeatExecutor() {
        return heartBeatExecutor;
    }
}
